package book.dao.admin.impl;


import book.dao.admin.AdminDao;
import book.exception.SystemBookDbException;

import book.pojo.Admin;
import book.util.DbHelper;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class AdminDaoImpl implements AdminDao {
    @Override
    public Admin findAdminByUserName(String username) {
        try {
            Admin Admin = null;
            //1、创建Query对象，将数据源对象为参数，runner内部使用完连接会自动返回连接池
            QueryRunner runner = new QueryRunner(DbHelper.getDataSource());
            //2、编写sql
            String sql = "select * from Admin where username=?";
            //3、执行查询
            Admin = runner.query(sql,new BeanHandler<Admin>(Admin.class),username);
            if(Admin == null){
                String sql2 = "select * from admin where username=?";
                Admin = runner.query(sql2,new BeanHandler<Admin>(Admin.class),username);
            }
            return Admin;
        } catch (SQLException e) {
            throw new SystemBookDbException("根据id用户错误："+e.getMessage());
        }
    }
}
